import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:date_format/date_format.dart';

class DateView extends StatefulWidget {
  const DateView({Key? key}) : super(key: key);

  @override
  _DateViewState createState() => _DateViewState();
}

class _DateViewState extends State<DateView> {
  var _date = DateTime.now();
  TimeOfDay _nowTime = TimeOfDay(hour: 12, minute: 0);
  var _thirdDate = DateTime.now();

  _getDate() {
    showDatePicker(
            context: context,
            initialDate: _date,
            firstDate: DateTime(1980),
            lastDate: DateTime(2222))
        .then((value) {
      setState(() {
        _date = value!;
      });
    });
  }

  void _chooseTime() async {
    var result = await showTimePicker(
        context: context, initialTime: TimeOfDay(hour: 12, minute: 10));

    setState(() {
      _nowTime = result!;
    });
  }

  void _showThirdDatePicker() {
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(2008, 3, 5),
        maxTime: DateTime(2030, 6, 7), onChanged: (date) {
      print('change $_thirdDate');
    }, onConfirm: (date) {
      setState(() {

        _thirdDate= date;

      });
      print('confirm $_thirdDate');
    }, currentTime: DateTime.now(), locale: LocaleType.zh);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          SizedBox(
            height: 100,
          ),
          Text('自带的日期选择控件'),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(formatDate(_date, [yyyy, '-', mm, '-', dd])),
                    Icon(Icons.keyboard_arrow_down)
                  ],
                ),
                onTap: () {
                  // setState(() {
                  _getDate();
                  //print("日期${_date}");
                  // });
                },
              ),
              InkWell(
                child: Row(
                  children: [
                    Text("${_nowTime.format(context)}"),
                    Icon(Icons.keyboard_arrow_down)
                  ],
                ),
                onTap: () {
                  _chooseTime();
                },
              )
            ],
          ),
          SizedBox(
            height: 50,
          ),
          Text('第三方日期选择控件'),
          InkWell(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(formatDate(_thirdDate,
                    [yyyy, '-', mm, '-', dd, '  ', HH, ':', nn, ':', ss])),
                Icon(Icons.arrow_drop_down)
              ],
            ),
            onTap: () {
              _showThirdDatePicker();
            },
          )
        ],
      ),
    );
  }
}
